<?php

namespace app\admin\controller\goods;

use app\common\controller\Backend;
use fast\Tree;
use app\common\model\goods\Cate as CateModel;
use think\Db;

/**
 * 
 *
 * @icon fa fa-circle-o
 */
class Cate extends Backend
{
    
    /**
     * Cate模型对象
     * @var \app\common\model\goods\Cate
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\common\model\goods\Cate;

        $tree = Tree::instance();
        $tree->init(collection($this->model->order('weigh desc,id desc')->select())->toArray(), 'pid');
        $this->categorylist = $tree->getTreeList($tree->getTreeArray(0), 'name');
        $categorydata = [0 => ['type' => 'all', 'name' => __('None')]];
        foreach ($this->categorylist as $k => $v) {
            $categorydata[$v['id']] = $v;
        }
        $typeList = CateModel::getTypeList();
        $this->view->assign("typeList", $typeList);
        $this->view->assign("parentList", $categorydata);
        $this->assignconfig('typeList', $typeList);
    }
    
    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
    

    /**
     * 查看
     */
    public function index()
    {

        //当前是否为关联查询
        $this->relationSearch = false;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax())
        {

            $search = $this->request->request("search");
            $type = $this->request->request("type");

            //构造父类select列表选项数据
            $list = [];
            foreach ($this->categorylist as $k => $v) {
                if ($search) {
                    if ($v['type'] == $type && stripos($v['name'], $search) !== false || stripos($v['nickname'], $search) !== false) {
                        if ($type == "all" || $type == null) {
                            $list = $this->categorylist;
                        } else {
                            $list[] = $v;
                        }
                    }
                } else {
                    if ($type == "all" || $type == null) {
                        $list = $this->categorylist;
                    } elseif ($v['type'] == $type) {
                        $list[] = $v;
                    }
                }
            }

            $total = count($list);
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }
    public function add(){
        if ($this->request->isAjax())
        {
            $params = input();
            $data = $params['row'];
            $num = $this->model->where(array('pid'=>$data['pid']))->count();
            $data['number'] = getnumbers($num);
            $res = $this->model->save($data);
            if($res){
                $this->success('添加成功');
            }
            $this->error('添加失败');
        }
        return $this->view->fetch();
    }
    public function del($ids = null)
    {
        $row = $this->model->find($ids);
        if(!$row){
            $this->error('非法操作');
        }
        if($row['is_auto'] == 1){
            $this->error('系统分类,禁止删除');
        }
        $res = $this->model->where(array('id'=>$ids))->delete();
        if($res){
            $this->success('删除成功');
        }else{
            $this->error('删除失败');
        }
    }
}
